package com.sx.first.dao;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.sx.first.dao.EmployeeDAO;
import com.sx.first.model.Employee;

@Repository("employeeDAO")
@Scope("prototype")
public class EmployeeDAO {
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public List<Employee> findAll() {
		@SuppressWarnings("unchecked")
		List<Employee> list = sessionFactory.getCurrentSession().createQuery("from Employee").list();
		return list;
	}

	public void save(Employee emp) {
		sessionFactory.getCurrentSession().save(emp);
	}

	public void delete(Employee emp) {
		sessionFactory.getCurrentSession().delete(emp);
	}

	public void update(Employee emp) {
		sessionFactory.getCurrentSession().update(emp);
	}

	public Employee findById(int empId) {
		Employee emp = sessionFactory.getCurrentSession().get(Employee.class, empId);
		Hibernate.initialize(emp.getPos());
		return emp;
	}

	public List<Employee> findApply() {
		@SuppressWarnings("unchecked")
		List<Employee> list = sessionFactory.getCurrentSession().createQuery("from Applystorage as a group by a.emp.empId").list();
		return list;
	}

}
